package com.zut.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.zut.entity.PaymentInfo;
import com.zut.enums.PayType;
import com.zut.mapper.PaymentInfoMapper;
import com.zut.service.PaymentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
    /**
     * 创建支付日志信息
     * @param plainText
     */
    // 记录支付日志  也就是说把明文中的信息进行存储进数据库当中
    @Override
    public void createPaymentInfo(String plainText) {
        log.info("支付日志: {}",plainText);
        // 从明文中获取需要的数据；
        Gson gson = new Gson();
        HashMap plainTextMap = gson.fromJson(plainText, HashMap.class);
        // 获取订单号
        String orderNo = (String) plainTextMap.get("out_trade_no");
        // 获取支付唯一编号
        String transactionId = (String)plainTextMap.get("transaction_id");
        // 获取支付类型
        String tradeType = (String)plainTextMap.get("trade_type");
        // 获取支付状态
        String tradeState = (String) plainTextMap.get("trade_state");
        // 实际支付总金额
        Map<String,Object> amount = (Map<String,Object>)plainTextMap.get("amount");
        int payerTotal = ((Double) amount.get("payer_total")).intValue();
        // 封装数据
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderNo(orderNo);
        paymentInfo.setTransactionId(transactionId);
        paymentInfo.setPaymentType(PayType.WXPAY.getType());  // 用微信支付的，而非支付宝
        paymentInfo.setTradeType(tradeType);
        paymentInfo.setTradeState(tradeState);
        paymentInfo.setPayerTotal(payerTotal);
        paymentInfo.setContent(plainText);
        baseMapper.insert(paymentInfo);
    }
}
